
<script type="text/html" data-template-name="data-generator">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
    </div>
    <div class="form-row">
        <label for="node-input-field"><i class="fa fa-edit"></i> <span data-i18n="node-red:template.label.property"></span></label>
        <input type="text" id="node-input-field" placeholder="payload" style="width:275px;">
        <input type="hidden" id="node-input-fieldType">
    </div>
    <div class="form-row" style="position: relative; margin-bottom: 0px;">
        <label for="node-input-template"><i class="fa fa-file-code-o"></i> <span data-i18n="node-red:template.label.template"></span></label>
        <input type="hidden" id="node-input-template" autofocus="autofocus">
    </div>
    <div class="form-row node-text-editor-row">
        <div style="height:250px;" class="node-text-editor" id="node-input-template-editor"></div>
    </div>
    <div class="form-row">
        <label for="node-input-syntax"><i class="fa fa-code"></i> <span data-i18n="datagen.label.syntax"></span></label>
        <select id="node-input-syntax" style="width:180px;">
            <option value="text" data-i18n="datagen.label.text"></option>
            <option value="json" data-i18n="datagen.label.json"></option>
        </select>
    </div>
</script>

<script type="text/javascript">
    RED.nodes.registerType('data-generator',{
        color:"rgb(243, 181, 103)",
        category: 'function',
        defaults: {
            name: {value:""},
            field: {value:"payload"},
            fieldType: {value:"msg"},
            syntax: {value:"text"},
            template: {value:"{\n    \"name\": \"{{firstName}} {{lastName}}\",\n    \"work\": \"{{company}}\",\n    \"email\": \"{{email}}\",\n    \"address\": \"{{int 1 100}} {{street}}\",\n    \"country\": \"{{country}}\"\n}"}
        },
        inputs:1,
        outputs:1,
        icon: "template.png",
        paletteLabel: function() {
            return this._("datagen.datagen");
        },
        label: function() {
            return this.name || "data generator";
        },
        outputLabels: function() { return this.syntax === "json" ? "object" : "string"; },
        oneditprepare: function() {
            var that = this;
            if (!this.fieldType) {
                this.fieldType = 'msg';
            }
            $("#node-input-field").typedInput({
                default: 'msg',
                types: ['msg','flow','global'],
                typeField: $("#node-input-fieldType")
            });

            this.editor = RED.editor.createEditor({
                id: 'node-input-template-editor',
                mode: 'ace/mode/handlebars',
                value: $("#node-input-template").val()
            });
            RED.library.create({
                url:"functions", // where to get the data from
                type:"function", // the type of object the library is for
                editor:that.editor, // the field name the main text body goes to
                fields:['name','outputs']
            });
            this.editor.focus();
        },
        oneditsave: function() {
            $("#node-input-template").val(this.editor.getValue());
            this.editor.destroy();
            delete this.editor;
        },
        oneditcancel: function() {
            this.editor.destroy();
            delete this.editor;
        },
        oneditresize: function(size) {
            var rows = $("#dialog-form>div:not(.node-text-editor-row)");
            var height = $("#dialog-form").height();
            for (var i=0; i<rows.size(); i++) {
                height -= $(rows[i]).outerHeight(true);
            }
            var editorRow = $("#dialog-form>div.node-text-editor-row");
            height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
            $(".node-text-editor").css("height",height+"px");
            this.editor.resize();
        }
    });
</script>
